﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMSGSC.BLL;
using PMSGSC.DAL;
using PMSGSC.Common;
using DevExpress.XtraEditors.Controls;

namespace PMSGSC.App
{
    public partial class frmWorkOrder : DevExpress.XtraEditors.XtraForm
    {
        frmMain main = new frmMain();
        public frmWorkOrder(frmMain objMain)
        {
            InitializeComponent();
            main = objMain;
            employeeList = Employee_BLL.GetAll(frmMain.ship.Ship_ID);
            foreach (Employee e in employeeList.Cast<Employee>().ToList())
            {
                CheckedListBoxItem ckBox = new CheckedListBoxItem(e.Employee_ID, e.Name);
                disciplinesList.Items.Add(ckBox);
            }
        }
        bool isStop = true;
        private List<WorkOrder> workOrderList = new List<WorkOrder>();
        private List<Employee> employeeList;
        private void btnOK_Click(object sender, EventArgs e)
        {
           if((deFrom.Text != "" && deTo.Text == "") ||(deFrom.Text == "" && deTo.Text != ""))
           {
               XtraMessageBox.Show("Please choose full datetime From & To", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               return;
           }
            if (isStop)
            {
                frmProcessBar pb = new frmProcessBar();
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
                {
                    LoadSkinToForm();
                    pb.ShowDialog();
                }
                ));
                thread.Start();
                isStop = false;

                if (ucLoadEquipment1.lbid.Text == "")
                    workOrderList = WorkOrder_BLL.GetWork(txtNumber.Text, txtTitle.Text, 0, frmMain.ship.Ship_ID);
                else
                    workOrderList = WorkOrder_BLL.GetWork(txtNumber.Text, txtTitle.Text, int.Parse(ucLoadEquipment1.lbid.Text), frmMain.ship.Ship_ID);

                int disciplinesFlag = 0;
                if (disciplinesList.Items.GetCheckedValues().Count > 0)
                {
                    for (int i = 0; i < workOrderList.Count; i++)
                    {
                      
                        foreach (CheckedListBoxItem chkItem in disciplinesList.CheckedItems)
                        {
                            if (workOrderList[i].RespDescipline_ID == int.Parse(chkItem.Value.ToString()))
                            {
                                disciplinesFlag = 1;
                            }
                        }
                        if (disciplinesFlag == 0)
                        {
                            workOrderList.RemoveAt(i);
                            i--;
                        }
                        else
                        {
                            disciplinesFlag = 0;
                        }
                    }
                }

                int planFlag = 0;
                if(PlanCheckBoxList.Items.GetCheckedValues().Count > 0)
                {
                    for (int i = 0; i < workOrderList.Count; i++)
                    {
                        if (workOrderList[i].NextDue.Value != null)
                        {
                            foreach (CheckedListBoxItem chkItem in PlanCheckBoxList.CheckedItems)
                            {
                                DateTime temp;
                                switch (chkItem.Value.ToString())
                                {
                                    case "0":
                                        temp = DateTime.Now.Date;
                                        if (workOrderList[i].NextDue.Value.Date.CompareTo(temp) == 0)
                                            planFlag = 1;

                                        break;
                                    case "1":
                                        if (deFrom.Text == "" && deTo.Text == "")
                                        {
                                            temp = DateTime.Now.Date;
                                            if (workOrderList[i].NextDue.Value.Date.CompareTo(temp) < 0)
                                                planFlag = 1;
                                        }
                                        else
                                        {
                                            if (workOrderList[i].NextDue.Value.Date.CompareTo(deFrom.DateTime) >= 0 && workOrderList[i].NextDue.Value.Date.CompareTo(deTo.DateTime) <= 0)
                                                planFlag = 1;
                                        }

                                        break;
                                    case "2":
                                        temp = GetDayOfFirstWeek(DateTime.Now.DayOfWeek);
                                        if (workOrderList[i].NextDue.Value.CompareTo(temp.Date) >= 0 && workOrderList[i].NextDue.Value.CompareTo(temp.AddDays(6).Date) <= 0)
                                            planFlag = 1;

                                        break;
                                    case "3":
                                        temp = GetDayOfLastWeek(DateTime.Now.DayOfWeek);
                                        if (workOrderList[i].NextDue.Value.CompareTo(temp.Date) >= 0 && workOrderList[i].NextDue.Value.CompareTo(temp.AddDays(6).Date) <= 0)
                                            planFlag = 1;
                                        break;
                                }
                                if (planFlag == 0)
                                {
                                    workOrderList.RemoveAt(i);
                                    i--;
                                }
                                else
                                {
                                    planFlag = 0;
                                }
                            }
                        }
                    }
                }
                if (workOrderList.Count > 0)
                {
                    frmWorkOrderDeatails workOrderDetails = new frmWorkOrderDeatails(workOrderList, main);
                    workOrderDetails.MdiParent = main;
                    workOrderDetails.Show();

                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    this.Close();
                }
                else
                {
                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    XtraMessageBox.Show("Not result in display", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private static void LoadSkinToForm()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Office 2007 Blue");
            }
        }
        private DateTime GetDayOfFirstWeek(DayOfWeek dow)
        {
            DateTime dayOfFirstWeek = DateTime.Now;
            switch (dow)
            {
                case DayOfWeek.Monday:
                    dayOfFirstWeek=  DateTime.Now;
                    break;
                case DayOfWeek.Tuesday:
                    dayOfFirstWeek =  DateTime.Now.AddDays(-1);
                    break;
                case DayOfWeek.Wednesday:
                    dayOfFirstWeek =  DateTime.Now.AddDays(-2);
                    break;
                case DayOfWeek.Thursday:
                    dayOfFirstWeek =  DateTime.Now.AddDays(-3);
                    break;
                case DayOfWeek.Friday:
                    dayOfFirstWeek = DateTime.Now.AddDays(-4);
                    break;
                case DayOfWeek.Saturday:
                    dayOfFirstWeek = DateTime.Now.AddDays(-5);
                    break;
                case DayOfWeek.Sunday:
                    dayOfFirstWeek = DateTime.Now.AddDays(-6);
                    break;
            }
            return dayOfFirstWeek;
        }
        private DateTime GetDayOfLastWeek(DayOfWeek dow)
        {
            DateTime dayOfLastWeek = DateTime.Now;
            switch (dow)
            {
                case DayOfWeek.Monday:
                    dayOfLastWeek = DateTime.Now.AddDays(7);
                    break;
                case DayOfWeek.Tuesday:
                    dayOfLastWeek = DateTime.Now.AddDays(6);
                    break;
                case DayOfWeek.Wednesday:
                    dayOfLastWeek = DateTime.Now.AddDays(5);
                    break;
                case DayOfWeek.Thursday:
                    dayOfLastWeek = DateTime.Now.AddDays(4);
                    break;
                case DayOfWeek.Friday:
                    dayOfLastWeek = DateTime.Now.AddDays(3);
                    break;
                case DayOfWeek.Saturday:
                    dayOfLastWeek = DateTime.Now.AddDays(2);
                    break;
                case DayOfWeek.Sunday:
                    dayOfLastWeek = DateTime.Now.AddDays(1);
                    break;
            }
            return dayOfLastWeek;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                this.Close();
            }
        }
    }
}